package com.bdqn.tour.servlet.Hzy;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bdqn.tour.pojo.HzyTraveler_documents;
import com.bdqn.tour.pojo.HzyTravelers;
import com.bdqn.tour.service.HzyTravelersService;
import com.bdqn.tour.service.impl.HzyTravelersServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/addTraveler")
public class AddTravelerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");

        // 读取请求体中的 JSON 数据
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = req.getReader();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }

        // 解析 JSON 数据
        JSONObject jsonData = JSONObject.parseObject(sb.toString());

        // 1. 构建 travelers 对象
        HzyTravelers traveler = new HzyTravelers();
        traveler.setChineseName(jsonData.getString("chineseName"));
        traveler.setSurname(jsonData.getString("surname"));
        traveler.setGivenName(jsonData.getString("givenName"));
        traveler.setGender(jsonData.getString("gender"));
        traveler.setNationality(jsonData.getString("nationality"));

        // 处理生日日期
        String birthYear = jsonData.getString("birthYear");
        String birthMonth = jsonData.getString("birthMonth");
        String birthDay = jsonData.getString("birthDay");
        String birthDate = birthYear + "-" + birthMonth + "-" + birthDay;
        traveler.setBirthDate(birthDate);

        traveler.setPhone(jsonData.getString("phone"));
        traveler.setEmail(jsonData.getString("email"));
        traveler.setDefault(jsonData.getBooleanValue("isDefault"));
        // 2. 处理证件信息
        JSONArray documents = jsonData.getJSONArray("documents");
        if (documents != null && !documents.isEmpty()) {
            List<HzyTraveler_documents> docList = new ArrayList<>();
            for (int i = 0; i < documents.size(); i++) {
                JSONObject doc = documents.getJSONObject(i);
                HzyTraveler_documents document = new HzyTraveler_documents();
                document.setDocument_type(doc.getString("document_type"));
                document.setDocument_number(doc.getString("document_number"));
                document.setExpiry_date(doc.getString("expiry_date"));
                docList.add(document);
            }
            traveler.setDocuments(docList);
        }
        // 3. 调用service保存
        HzyTravelersService travelersService = new HzyTravelersServiceImpl();
        int result = travelersService.addTraveler(traveler);
        // 4. 返回结果
        JSONObject jsonResult = new JSONObject();
        if (result > 0) {
            jsonResult.put("flag", true);
            jsonResult.put("message", "添加成功");
        } else {
            jsonResult.put("flag", false);
            jsonResult.put("message", "添加失败");
        }
        resp.getWriter().write(jsonResult.toString());
    }
}
